package com.dha.asynctask;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.dha.caodangcongnghe.R;
import com.dha.variable.DangNhapVariable;
import com.dha.variable.ThongBaoVariable;
import com.dha.variable.ThongTinTaiKhoan;

public class LayTenSinhVienAsyncTask extends AsyncTask<Void, String, Boolean> {
	private Activity activity;
	private String maSo;

	private TextView tvTenSinhVien, tvMaSinhVien;
	
	public LayTenSinhVienAsyncTask(Activity activity, String maSo) {
		super();
		this.activity = activity;
		this.maSo = maSo;
		
		tvMaSinhVien = (TextView) this.activity.findViewById(R.id.tv_MaSV_Diem);
		
	}

	@Override
	protected void onProgressUpdate(String... values) {
		ThongTinTaiKhoan.hoTenSinhVien = values[0].toString();
		tvTenSinhVien = (TextView) activity
				.findViewById(R.id.tv_tenSV_Diem);
		tvTenSinhVien.setText(ThongTinTaiKhoan.hoTenSinhVien);
		super.onProgressUpdate(values);
	}

	@Override
	protected Boolean doInBackground(Void... params) {

		try {
			// Nếu đăng nhập ok thì tiến hành lấy ra thông tin mã tài khaosrn và
			// họ tên
			SoapObject requestThongTin = new SoapObject(
					DangNhapVariable.NAME_SPACE_DANGNHAP,
					DangNhapVariable.METHOD_NAME_THONGTIN);

			PropertyInfo maSoinfoThongTin = new PropertyInfo();
			maSoinfoThongTin.setName("MaSo");
			maSoinfoThongTin.setValue(maSo);
			requestThongTin.addProperty(maSoinfoThongTin);

			SoapSerializationEnvelope envelopeThongTin = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelopeThongTin.setOutputSoapObject(requestThongTin);

			HttpTransportSE httpTransportSEThongTin = new HttpTransportSE(
					DangNhapVariable.URL_DANGNHAP);
			httpTransportSEThongTin.call(DangNhapVariable.SOAP_ACTION_THONGTIN,
					envelopeThongTin);

			SoapPrimitive responeThongTin = (SoapPrimitive) envelopeThongTin
					.getResponse();
			if (responeThongTin != null) {
				// Lưu thông tin vào các biến static
				publishProgress(responeThongTin.toString());
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

	@Override
	protected void onPostExecute(Boolean result) {
		AlertDialog.Builder builder = null;
		builder = new Builder(activity);
		builder.setNegativeButton("Đóng", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				activity.finish();
			}
		}).setTitle("Lỗi");
		AlertDialog alertDialog = null;
		if (result == false){
			builder.setMessage("Mã sinh viên "+maSo+" không tồn tại");
			alertDialog = builder.create();
			alertDialog.show();
		}else{
			tvMaSinhVien.setText(ThongTinTaiKhoan.maSinhVien);
		}
		super.onPostExecute(result);
	}

}
